$(function(){
	anime_index.run();
});

var anime_index = {
	run: function(){
		this._init_();
		this._start_();
		this._bind_events();
	},
	
	_init_: function(){
		this.hasMore = $("#show_content_div > :hidden.hasMore").val() || false;
		this.category = $("#category").val() || 'all';
		this.genre = "all";
		this.initial = "";
		this.pageSize = 30;
		this.scrollPage = 2;
	},
	
	_start_: function(){		
		this._highlight_nav();
		
		var loc = location.href;
		var index = loc.indexOf("#");
		if(index > 0){
			var params = loc.substring(index + 1);
			this._highlight_genre_or_alpha(params);
			$("#show_content_div").html($("#loading_div").html());
			this._query_animes(1,params);
		}
		this._adjust_layout();
	},
	
	_bind_events: function(){
		//genre
		$("#genres_ul > li > a").click(function(){
			var $this = $(this);
			var g = $this.attr("genre");
			$("#genres_ul > li > a").removeClass("selected");
			$this.addClass("selected");
			$("#alpha_div > a").removeClass("selected");
			$("#alpha_div > a").eq(0).addClass("selected");
			anime_index._handle_genre_clicked(g);		
			return false;
		});

		//initial
		$("#alpha_div > a").click(function(){
			var $this = $(this);
			var initial = $this.attr("alpha");
			$("#alpha_div > a").removeClass("selected");
			$this.addClass("selected");
			anime_index._handle_alpha_clicked(initial);
			return false;
		});
		
		//scrolling
		$.scroll(function(){
			anime_index._handle_scrolling();
		});
						
		$(window).wresize(function() {
			anime_index._adjust_layout();
		});
		
		$("#open_side_btn").click(function(){
			anime_index._adjust_layout("open");
		});
		
		$("#close_side_btn").click(function(){
			anime_index._adjust_layout();
		});
		
		$("#gotoTop").easeup();
		anime_index._handle_index_to_detail_location_param();
	},
	

	_highlight_nav: function(){
		var path = location.pathname;
		var $li = $("#side_menu > ul > li");
		
		if(path.startWith(main.contextPath + "/world/anime/updated")){
			$li.eq(2).addClass("curr");
		}else if(path.startWith(main.contextPath + "/world/anime/simulacasts")){
			$li.eq(1).addClass("curr");
		}else{
			$li.eq(0).addClass("curr");
		}
	},
	
	_highlight_genre_or_alpha: function(params){
		var paramArr = params.split("&");
		for(var i=0; i<paramArr.length;i++){
			if (paramArr[i].indexOf("genre") >= 0) {
				var value = paramArr[i].substring(paramArr[i].indexOf("=") + 1);
				$("#genres_ul > li > a").removeClass("selected");
				$("#genres_ul > li > a").each(function() {
					if ($(this).attr("genre") == decodeURI(value)) {
						$(this).addClass("selected");
						anime_index.genre = decodeURI(value);
					}
				});
			} else if (paramArr[i].indexOf("initial") >= 0) {
				var value = paramArr[i].substring(paramArr[i].indexOf("=") + 1);
				$("#alpha_div > a").removeAttr("class");
				$("#alpha_div > a").each(function() {
					if ($(this).attr("alpha") == decodeURI(value)) {
						$(this).addClass("selected");
						anime_index.initial = decodeURI(value);
					}
				});
			} 
		}
	},
	
	//handle index to detail location param
	_handle_index_to_detail_location_param: function(){
		$("#show_content_div > div.m_box5 > div.img > a").click(function(){
			var href = $(this).attr("href");
			if(anime_index.category == "all"){
				location.href = href + "#genre=" + anime_index.genre + "&category=all";
			}else{
				location.href = href + "#category=" + anime_index.category;
			}
			return false;
		});
		$("#show_content_div > div.m_box5 > ul > li > a").click(function(){
			var href = $(this).attr("href");
			if(anime_index.category == "all"){
				location.href = href + "#genre=" + anime_index.genre + "&category=all";
			}else{
				location.href = href + "#category=" + anime_index.category;
			}
			return false;
		});
	},
	
	_handle_genre_clicked: function(genre){		
		if(genre != this.genre){
			this.genre = genre;
			this.initial = null;
			this._change_the_location();
			//reset scrollPage
			this.scrollPage = 1;	
			//reset content holder
			$("#show_content_div").html($("#loading_div").html());
			
			this._query_animes();			
		}
	},
	
	_handle_alpha_clicked: function(initial){
		if(initial != this.initial){
			this.initial = initial;
			this._change_the_location();
			this.scrollPage = 1;
			$("#show_content_div").html($("#loading_div").html());
			this._query_animes();
		}
	},
	
	_change_the_location: function(){
		var loc = location.href;
		var params = this._gather_params();
		if(params.length > 0){
			loc = loc.substring(0,loc.indexOf("#"));
			location.href = loc + "#" + params; 
		}
		return false;
	},
	
	_handle_scrolling: function(){			
		if(this.hasMore){
			var $w = $(window);
			if( ! $w.data("scrolling")){
				$w.data("scrolling", true);
				anime_index._query_animes(this.scrollPage);
			}
		}
	},
	
	_query_animes: function(page,params){
		var $w = $(window);
		if($w.data("querying")) {
			return;
		}
		
		$w.data("querying", true);
		var $loading = $("#show_content_div > div.loadingBox");
		$loading.show();
		page = page || 1;
		var data = null;
		if(params){
			data = params;
		}else{
			data = this._gather_params();
		}
		var url = main.contextPath + "/world/anime/" + this.category + "/query?page=" + page;
		$.get(url, data, function(result){
			anime_index._check_has_more(result);
			$loading.before(result);
		}).complete(function(){
			$w.data("querying", false);
			$w.data("scrolling", false);
			$loading.hide();
			//重新绑定事件
			anime_index._adjust_layout();
			anime_index._handle_index_to_detail_location_param();
		});
	},
	
	_gather_params: function(){
		var ps = [];
		if(this.genre){
			ps.push("genre=" + this.genre);
		}
		if(this.initial){
			ps.push("initial=" + this.initial);
		}
		if(ps){
			return ps.join("&");
		}
	},
	
	_check_has_more: function(result){
		var $result = $("<div>" + result + "</div>");
		var m = $(":hidden.hasMore", $result).val();
		if(m){
			this.hasMore = true;
			this.scrollPage += 1;
		}else{
			this.hasMore = false;
		}
	},
	
	_adjust_layout: function(action){
		var boxWidth = $("#show_content_div").width()-5;
		if(action == 'open'){
			boxWidth = boxWidth - 180;
		}
		var account = Math.floor(boxWidth/160);
		var paddingValue = 20 + (boxWidth - 160*account + 20)/(account-1);
		var flag = 1;
		$("#show_content_div > div.m_box5").each(function(){
			$(this).css("padding-right",0);
			if(flag < account){
				if(flag == 1){
					$(this).css("padding-left",0);
					flag++;
				}else{
					$(this).css("padding-left",paddingValue);
					flag++;
				}
			}else{
				$(this).css("padding-left",paddingValue);
				flag = 1;
			}
		});
	}
};